// pages/hotelDetailSearch/hotelDetailSearch.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    h_id: "",
    collectionDetail: [],
    cart: [],
    showCart: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.getStorage({
      key: 'hotelCart',
      success(res) {
        console.log(res.data)
        that.setData({
          value: options.value,
          h_id: options.h_id,
          cart: res.data
        })
        that.updataPrice()
        that.getList()
      }
    })
  },
  onUnload() {
    wx.setStorage({
      key: "hotelCart",
      data: this.data.cart
    })
  },
  getList() {
    const _ = wx.cloud.database().command
    if (this.data.value == "") {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
    } else {
      app.db("menu").where(
          _.and([{
              h_id: this.data.h_id
            },
            {
              name: wx.cloud.database().RegExp({
                regexp: this.data.value,
                options: 'i'
              })
            }
          ])
        ).get()
        .then(res => {
          console.log("查询成功", res.data)
          this.setData({
            collectionDetail: res.data
          })
        })
    }
  },
  onSearch(e) {
    this.setData({
      value: e.detail
    })
    this.getList()
  },
  //添加到购物车
  addToMenuCart(e) {
    if (wx.getStorageSync('userInfo')) {
      console.log(e.currentTarget.dataset)
      let cart = this.data.cart
      let num = 0
      let newCart = {}
      newCart.id = e.currentTarget.dataset.id
      newCart.imgSrc = e.currentTarget.dataset.imgsrc
      newCart.name = e.currentTarget.dataset.name
      newCart.price = e.currentTarget.dataset.price
      newCart.count = e.currentTarget.dataset.count
      for (let i = 0; i < cart.length; i++) {
        if (cart[i].id === e.currentTarget.dataset.id) {
          cart[i].count += e.currentTarget.dataset.count
          this.setData({
            cart: cart
          })
          num = 1
          break
        }
      }
      if (num === 0) {
        this.setData({
          cart: this.data.cart.concat(newCart)
        })
      }
      this.updataPrice()
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none"
      })
    }
  },
  // 跳转支付页面    
  goToPay() {
    if (wx.getStorageSync('userInfo')) {
      let cart = this.data.cart
      wx.setStorageSync('goods', cart)
      wx.navigateTo({
        url: '/pages/payinfo/payinfo?total=' + this.data.price + "&type=" + 3,
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none"
      })
    }
  },
  // 计算价格
  updataPrice() {
    let price = 0
    for (let i = 0; i < this.data.cart.length; i++) {
      console.log("价格为", this.data.cart[i].price)
      price += this.data.cart[i].price * this.data.cart[i].count
    }
    this.setData({
      price: price
    })
  },
  showCartPopup() {
    if (this.data.cart.length > 0) {
      this.setData({
        showCart: true
      });
    }
  },

  onCartClose() {
    this.setData({
      showCart: false
    });
  },
  onCountChange(e) {
    console.log(e)
    // console.log(this.data.cart)
    let cart = this.data.cart
    cart[e.currentTarget.dataset.index].count = e.detail
    this.setData({
      cart: cart
    })
    this.removeCart()
    this.updataPrice()

  },
  removeCart() {
    // console.log(this.data.cart)
    let cart = this.data.cart
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].count === 0) {
        // console.log('该删')
        cart.splice(i, 1)
      }
    }
    this.setData({
      cart: cart  
    })
  },
})